package top.jlzhou.tools;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Objects;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializeWriter;

/**
 * <pre>FastJSON的long类型转换成字符串表示
 * 	在Spring中配置该类
 * </pre>
 * @ClassName	top.jlzhou.tools.FastJSONLong
 * @author	J.L.Zhou
 * @date	2019年3月9日 下午6:49:16
 * @version 	V1.0
 * @Copyright	2019 jlzhou.top Inc. All rights reserved. 
 * 	注意：本内容仅限于公司内部传阅，禁止外泄以及用于其他的商业目
 */
public class FastJSONLong {

	static {
		
		ObjectSerializer serializer = new ObjectSerializer() {
            @Override
            public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
                    throws IOException {
                SerializeWriter out = serializer.getWriter();

                if (fieldType == long.class || fieldType == Long.class || object instanceof Long) {
                    out.writeString(Objects.toString(object, null));
                }
            }
        };
        
		SerializeConfig.getGlobalInstance().put(Long.class, serializer);
		SerializeConfig.getGlobalInstance().put(long.class, serializer);
	}
}
